﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TaskSystemClient.SPM;

namespace TaskSystemClient
{
    public partial class ChooseMileStone : Form
    {
        public ChooseMileStone()
        {
            InitializeComponent();
            LoadMileStones();
        }

        Dictionary<string, TreeNode> nodeMap = new Dictionary<string, TreeNode>();
        private void LoadMileStones()
        {
            tvMileStone.Nodes.Add("<空里程碑>");

            MileStone[] mileStones = MileStoneManager.Instance.MileStones;

            foreach (var mileStone in mileStones)
            {
                TreeNode node = new TreeNode(mileStone.Name);
                node.Tag = mileStone;
                TreeNode parentNode = null;
                int pos = mileStone.Code.LastIndexOf(")(");

                if (pos != -1)
                {
                    string parentCode = mileStone.Code.Substring(0, pos + 1);
                    if (nodeMap.ContainsKey(parentCode))
                        parentNode = nodeMap[parentCode];
                }
                if (parentNode == null)
                    tvMileStone.Nodes.Add(node);
                else
                    parentNode.Nodes.Add(node);

                nodeMap.Add(mileStone.Code, node);
            }

            tvMileStone.ExpandAll();
        }

        private void OnOK(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
        public string SelectedMileStoneCode
        {
            get
            {
                if (tvMileStone.SelectedNode == null)
                    return null;
                MileStone selectedMileStone=tvMileStone.SelectedNode.Tag as MileStone;
                if(selectedMileStone==null)
                    return null;
                return selectedMileStone.Code;
            }
            set
            {
                TreeNode node = null;
                if (!string.IsNullOrEmpty(value) && nodeMap.ContainsKey(value))
                {
                    node = nodeMap[value];
                }
                tvMileStone.SelectedNode = node;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void ChooseMileStone_Load(object sender, EventArgs e)
        {
            Location = GameEditor.Common.Utilities.GetPopupPosition(Cursor.Position, Size);

        }

    }
}
